var wxpay = require('../../utils/pay.js')
var app = getApp()
var self, comp;
let datajson,opkey="";
import HttpUtil from '../../lib/trilobite/core/httputil.js'


/*
 * 订单列表访问
*/

class getGoodsList {
  constructor() {
    this.http = new HttpUtil(app);
    this.http.addResultListener(this.result);
  }
  result = (res) => {
    if (this.callback) {
      this.callback(res);
    }
  }
  /**
   * 加载接口
   */
  load = (e) => {
    this.http.post("/FindGoodsByCondition", {  start: 1, pagesize: 15,...e })
  }
}

/**
 * 页面控制器
 */
class PageController {
  constructor() {
    comp = this;
    comp.getGoodsList = new getGoodsList();
    comp.getGoodsList.callback = this.getGoodsList_callback;
  }
  getGoodsList_callback = (res) => {
    console.log(res)
    self.setData({
      goods: [],
      loadingMoreHidden: true
    });
    if (res.data.code != 200 || res.data.data.length == 0) {
      self.setData({
        loadingMoreHidden: false,
      });
      return;
    }

    self.setData({

      goods: res.data.data,
    });
  }

  data = {
    statusType: ["综合", "最新", "价格", "销量"],
    currentType: 0,
    goods:[],
    loadingMoreHidden: true,
    upflag:false,
    downflag:false,
    scrollTop:"0",
    searchInput:"",
    palacholderkey:"商品列表"
    
  }
  //看详情
  toDetailsTap =(e) =>{
  wx.navigateTo({
    url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
  })
}
  //状态点击切换
  statusTap = (e) => {
    var curType = e.currentTarget.dataset.index;
    self.data.currentType = curType
    self.setData({
      currentType: curType
    });
    //价格
    if (curType == 2){
      if(!self.data.upflag ){
        self.setData({
          upflag:true,
          downflag:false
        })
          datajson = {
            keyword: self.data.searchInput,
            byField: 'price',
            sortType: 'ASC',
            tagId: opkey
          }
      }else{
        self.setData({
          upflag: false,
          downflag: true
        })
          datajson = {
            keyword: self.data.searchInput,
            byField: 'price',
            sortType: 'DESC',
            tagId: opkey
          }
      }
      comp.getGoodsList.load(datajson);
    }
    //综合
    if (self.data.currentType === 0) {
        datajson = {
          keyword: self.data.searchInput,
          sortType: 'DESC',
          tagId: opkey
        }
      comp.getGoodsList.load(datajson);
    }
    //最新
    if (self.data.currentType === 1) {
        datajson = {
          keyword: self.data.searchInput,
          byField: 'date',
          sortType: 'DESC',
          tagId: opkey
        }
      comp.getGoodsList.load(datajson);
    }
    //销量
    if (self.data.currentType === 3) {
        datajson = {
          keyword: self.data.searchInput,
          byField: 'sv',
          sortType: 'DESC',
          tagId: opkey
        }
      comp.getGoodsList.load(datajson);
    }
  }
  scroll=(e)=> {
  var that = this, scrollTop = that.data.scrollTop;
  that.setData({
    scrollTop: e.detail.scrollTop
  })
}
  /**
   * 当页面显示的时候触发list对象的load方法
   */
  onShow = function () {

    //this.selectComponent("#list").load();
  }
  onLoad = function (options) {
    self = this;
    console.log(options)
    if (options.key){
      opkey = options.key;
      datajson = {
        tagId: options.key,
        sortType: 'ASC',
      }
    }
    if (options.keyword){
      self.setData({
        searchInput: options.keyword
      })
      datajson = {
        keyword: options.keyword,
        sortType: 'ASC',
      }
    }
    if (options.key == 13){
      self.setData({
        palacholderkey:'热门商品'
      })
      wx.setNavigationBarTitle({
        title: '热门商品'
      })
    }
    if (options.key == 14) {
      self.setData({
        palacholderkey: '新品上市'
      })
      wx.setNavigationBarTitle({
        title: '新品上市'
      })
    }

   
    comp.getGoodsList.load(datajson);
  }
  //监听input
  listenerSearchInput = (e) =>{
  self.setData({
    searchInput: e.detail.value
  })

}
//搜索
toSearch =() => {
  datajson={
    keyword: self.data.searchInput,
    byField:'',
    sortType:'ASC',
    tagId:opkey
  }
  comp.getGoodsList.load(datajson);
}

}

Page(new PageController());